'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:MaterialPurchaseCtrl
 * @description
 * # MaterialPurchaseCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('MaterialPurchaseCtrl', function ($scope, localStorageService, openApi, Util, $location, $routeParams, FormSubmit) {
        var id = $scope.process_id = $routeParams.id
        $scope.checkTypeList = [{
            key: "是",
            value: 1
        }, {
            key: "否",
            value: 0
        }];

        $scope.chooseinfo = function () {
            // 清掉关联的编号
            var params = $scope.param;
            Util.savaParamForReview(params, id);
            $location.path("/material/chooseWarehouse")
        };

        // 从清单中选
        $scope.chooseformlist = function () {

            // 清掉关联的编号
            var params = $scope.param;
            Util.savaParamForReview(params, id);
            $location.path("material/materiallist")
        };

        // 从计划中选
        $scope.chooseformplan = function () {
            if (!$scope.param.projectId) {
                Util.Toast("请先选择仓库");
                return;
            }

            var params = $scope.param;
            Util.savaParamForReview(params, id);

            $location.path("material/materialtotalplan/" + $scope.param.projectId)
        };


        $scope.deleteItem = function (index) {
            $scope.param.details.splice(index, 1);
            $scope.index = -1;
        };

        $scope.swipe = function (index) {
            $scope.index = index;
        }
        $scope.resetlist = function () {
            $scope.index = -1;
        }
        // 验证是否输入完整
        var checkPicker = function (data) {
            if (!data.quantity > 0) {
                Util.Toast("请输入数量");
                return;
            }
            if (!(data.price > 0 || data.price === 0)) {
                Util.Toast("请输入单价");
                return;
            }
            return true;
        };

        $scope.submit = function (operationId) {
            // 底部类型end
            var options = {};
            // 流程id
            options.id = id;
            // 根据operationId判断是草稿还是提交
            options.operationId = operationId;
            options.urlParam = openApi.materialPurchase;
            options.param = angular.copy($scope.param, {});
            options.param.purchases = angular.toJson($scope.param.details)
            delete options.param.details

            FormSubmit.submit(options, $scope);
        };
        $scope.validate = function () {
            if (!$scope.param.warehouseName) {
                Util.Toast("请选择仓库名称");
                return;
            }
            if (typeof $scope.param.needEntry === "undefined") {
                Util.Toast("请选择是否需要入库");
                return;
            }
            if (typeof $scope.param.directEntry === "undefined") {
                Util.Toast("请选择是否直接入库");
                return;
            }
            if (!$scope.param.purchaseReason) {
                Util.Toast("请填写采购事由");
                return;
            }
            if (!$scope.param.details) {
                Util.Toast("请添加材料采购明细");
                return;
            } else {
                var validate = true
                $scope.param.details.forEach(function (item) {
                    if (!checkPicker(item)) {
                        validate = false
                    }
                })
                if (!validate) {
                    Util.Toast("请完善材料采购明细必填项");
                    return;
                }
            }

            if (!$scope.validation.passvalidation) {
                Util.Toast($scope.validation.tip);
                return;
            }
            return true;
        }
        var init = function () {
            // 获取回显的详细信息
            Util.getDetailData(id, function (data) {
                $scope.param = data;
                var list1 = localStorageService.get("materialslist")
                var list2 = localStorageService.get("materialsTotalplan")
                localStorageService.remove("materialsTotalplan")
                localStorageService.remove("materialslist")
                const selectList = list2 || list1 || []
                var count = 0

                $scope.param.details = selectList.reduce(function (arr, current, index) {
                    const findItem = arr.find(function (item) {
                        return item.pkId === current.pkId
                    })
                    if (!findItem) {
                        arr.push(current)
                    } else {
                        count++
                    }
                    return arr
                }, $scope.param.details || [])

                if (count > 0) {
                    Util.Toast((selectList.length - count) + "条数据添加成功," + count + "条重复数据未添加", { time: 4000 })
                    count = 0
                }
            });
        };
        // 初始化
        init();
    });
